package com.linghu.imagetest;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.linghu.imagetest.util.FileUtil;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView iv_content;
    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_content=findViewById(R.id.iv_content);
        findViewById(R.id.btn_save).setOnClickListener(this);
        findViewById(R.id.btn_read).setOnClickListener(this);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_save) {
            String fileName = System.currentTimeMillis() + ".jpeg";
            path = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + File.separatorChar + fileName;
            Log.d("ning", path);
            Bitmap bitmap =
                    BitmapFactory.decodeResource(getResources(), R.drawable.img01);
            FileUtil.saveImage(path, bitmap);
        } else if (id == R.id.btn_read) {
            Bitmap b2 =
                    FileUtil.openImage(path);
            iv_content.setImageBitmap(b2);
        }
    }
}